package cn.ycc1.boot3redisson.user.service.impl;

import cn.ycc1.boot3redisson.annotation.MyRedissonLock;
import cn.ycc1.boot3redisson.annotation.Redisson;
import cn.ycc1.boot3redisson.user.domain.User;
import cn.ycc1.boot3redisson.user.mapper.UserMapper;
import cn.ycc1.boot3redisson.user.service.UserService;
import cn.ycc1.boot3redisson.user.vo.UserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
* @author mi
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-18 20:04:49
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public IPage<User> getPage(UserQueryVo queryVo) {
        IPage page = new Page(queryVo.getPageNum(), queryVo.getPageSize());
        return userMapper.getPage(page, queryVo);
    }

    @Override
    public User getDetail(Long id) {
        Assert.notNull(id, "id不能为空");
        return userMapper.selectById(id);
    }

    @Override
//    @MyRedissonLock(value = "my:redisson:lock", key = "#uid")
//    @MyRedissonLock(key = "'my:redisson:lock-'+#uid")
//    @MyRedissonLock
//    @MyRedissonLock(keyGenerator = "myKeyGeneratorByUid2")
    public boolean updateUser(long uid, long amount) {
        User user = this.getById(uid);
        user.setDeptId(user.getDeptId() + amount);
        return this.updateById(user);
    }

    /**
     * 更新用户信息
     * @param uid
     * @param amount
     * 注意参数类型：Long 与 long是不同的类型，否则可能反射时提示方法找不到问题
     */
    @Override
    @MyRedissonLock(key = "'my:redisson:lock-'+#uid")
//    @MyRedissonLock
    public void updateUserRedisson(Long uid, Long amount) {
        User user = this.getById(uid);
        user.setDeptId(user.getDeptId() + amount);
        this.updateById(user);
    }

}




